package com.ts.enterprise.module.user.service;

import com.ts.enterprise.common.service.CommonService;
import com.ts.enterprise.enums.PlatformEnum;
import com.ts.enterprise.exception.check.ResultNoExistException;
import com.ts.enterprise.module.user.entity.Worker;
import com.ts.enterprise.module.user.entity.WorkerInfo;
import com.ts.enterprise.module.user.query.SmsSendQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dean
 * @since 2021-10-18
 */
@Service
@Slf4j
public class MpUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private CommonService commonService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private WorkerService workerService;

    @Autowired
    private WorkerInfoService workerInfoService;


    public String smsLogin(SmsSendQuery query) {
        String mobile = query.getMobile();
//        String smsCode = query.getSmsCode();
//        commonService.checkSmsCode(mobile,smsCode);
        Worker worker = workerService.getByMobile(mobile);
        Optional.ofNullable(worker).orElseThrow(ResultNoExistException::new);
        Integer id = worker.getId();
        WorkerInfo workerInfo = workerInfoService.getByWorkerId(id);
        if (null==workerInfo){
            workerInfoService.saveInit(id);
        }
        return loginService.loginCheckUser(id,mobile, PlatformEnum.MP);
    }

    public Object getDetailById() {
        return null;
    }

    public String testLogin(Integer id) {
        Worker worker = workerService.getById(id);
        Optional.ofNullable(worker).orElseThrow(ResultNoExistException::new);
        return loginService.loginCheckUser(id, worker.getMobile(), PlatformEnum.MP);
    }
}
